package com.renchunlin.taobaounion.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.base.BaseFragment;
import com.renchunlin.taobaounion.model.domain.IBaseInfo;
import com.renchunlin.taobaounion.model.domain.SelectedContent;
import com.renchunlin.taobaounion.model.domain.SelectedPageCategory;
import com.renchunlin.taobaounion.presenter.ISelectedPagePresenter;
import com.renchunlin.taobaounion.presenter.ITicketPresenter;
import com.renchunlin.taobaounion.ui.activity.TicketActivity;
import com.renchunlin.taobaounion.ui.adapter.SelectedPageContentAdapter;
import com.renchunlin.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import com.renchunlin.taobaounion.utils.PresenterManager;
import com.renchunlin.taobaounion.utils.SizeUtils;
import com.renchunlin.taobaounion.utils.TicketUtil;
import com.renchunlin.taobaounion.view.ISelectedPageCallback;

import butterknife.BindView;

public class SelectedFragment extends BaseFragment implements ISelectedPageCallback {

    private ISelectedPagePresenter mSelectedPagePresenter;

    @BindView(R.id.left_category_list)
    RecyclerView leftCategoryList;

    private SelectedPageLeftAdapter mLeftAdapter;

    @BindView(R.id.right_content_list)
    RecyclerView rightCategoryRight;
    private SelectedPageContentAdapter mRightAdapter;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView barTitleTv;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout, container, false);
    }

    @Override
    protected void initView(View rootView) {
        setUpState(State.SUCCESS);
        leftCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));
        mLeftAdapter = new SelectedPageLeftAdapter();
        leftCategoryList.setAdapter(mLeftAdapter);

        rightCategoryRight.setLayoutManager(new LinearLayoutManager(getContext()));
        mRightAdapter = new SelectedPageContentAdapter();
        rightCategoryRight.setAdapter(mRightAdapter);
        rightCategoryRight.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(getContext(), 4);
                int leftAndRight = SizeUtils.dip2px(getContext(), 6);
                outRect.top = topAndBottom;
                outRect.left = leftAndRight;
                outRect.right = leftAndRight;
                outRect.bottom = topAndBottom;
            }
        });

        barTitleTv.setText("精选宝贝");
    }

    @Override
    protected void initPresenter() {
        mSelectedPagePresenter = PresenterManager.getInstance().getSelectedPagePresenter();
        mSelectedPagePresenter.registerViewCallback(this);
        mSelectedPagePresenter.getCategories();
    }

    @Override
    protected void initListener() {
        mLeftAdapter.setOnLeftItemClickListener(new SelectedPageLeftAdapter.OnLeftItemClickLisener() {
            @Override
            public void onLeftItemClick(SelectedPageCategory.DataBean item) {
                if (mSelectedPagePresenter != null) {
                    mSelectedPagePresenter.getContentByCategory(item);
                }
            }
        });

        mRightAdapter.setOnSelectedPageContentItemClickListener(new SelectedPageContentAdapter.OnSelectedPageContentItemClickListener() {
            @Override
            public void onContentItemClick(IBaseInfo item) {
                TicketUtil.toTicketPage(getContext(), item);
            }
        });
    }

    @Override
    protected void release() {
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onCategoriesLoaded(SelectedPageCategory categorys) {
        setUpState(State.SUCCESS);
        //分类内容
        mLeftAdapter.setData(categorys);
    }

    @Override
    public void onContentLoaded(SelectedContent content) {
        setUpState(State.SUCCESS);
        //分类具体内容
        mRightAdapter.setData(content);
        rightCategoryRight.scrollToPosition(0);
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {

    }

    @Override
    protected void onRetryClick() {
        //重试
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.reloadContent();
        }
    }
}
